#!/bin/bash
# Server starter Minecraft(papermc) server version 1.16.1 
# store it under ~/server dir
# 服务器启动脚本
# 存放在 ~/server 目录下

# Link colour table
source /home/pi/scripts/colourtable.sh


# Get time via founction
Rd() {
  date +"%Y-%m-%d %H:%M:%S"
}


Cpu_temp=`vcgencmd measure_temp`
Cpu_clock=`vcgencmd measure_clock arm`

clear

echo "Server time: "$(Rd)
echo "Server "$Cpu_temp
echo "Server "$Cpu_clock
sleep 1

echo ""
echo -e "====${GREEN}Picraft Starter${NC}================================================"
echo ""
echo "      Perform server stability test before running the server.     "
echo ""
echo "==================================================================="
echo ""
sleep 1
echo "It will take some time."

for f in {1..3}
do
    vcgencmd measure_temp
    vcgencmd measure_clock arm
    sysbench --test=cpu --cpu-max-prime=2500 --num-threads=4 run >/dev/null 2>&1
done

Cpu_temp=$(vcgencmd measure_temp | tr -dc '0-9')

if [ $Cpu_temp -ge 700 ]
then
  echo ""
  echo -e "====${RED}Picraft Starter${NC}================================================"
  echo ""
  echo "                  Fail to start Minecraft server                   "
  echo "                        Server is too hot!                         "
  echo ""
  echo "==================================================================="
  echo ""
  echo "[$(Rd)] Server too hot, Not safe to start the server. CPU_TEMP=$Cpu_temp " >> /home/pi/scripts/log.txt
  exit 1
fi

echo ""
echo -e "====${GREEN}Picraft Starter${NC}================================================"
echo ""
echo "                               Test passed                         "
echo "                             Ready to start...                     "
echo ""
echo "==================================================================="
echo ""
echo "[$(Rd)] Server started normally." >> /home/pi/scripts/log.txt
sleep 1

clear

echo "Starting time: "$(Rd)

screen -S picraft sudo java -Xms6G -Xmx6G -jar paper-135.jar